package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"
)

func producer (c chan int, cName int) {
	for {
		rand.Seed(time.Now().UnixNano())
		i := rand.Intn(1000)
		c<-i
		fmt.Printf("生产者 %d put ID: %d\n", cName, i)
		time.Sleep(2*time.Second)
	}

}
func producer1 (c chan int, cName int) {
	for {
		rand.Seed(time.Now().UnixNano())
		i := rand.Intn(1000)
		c<-i
		fmt.Printf("生产者 %d put ID: %d\n", cName, i)
		time.Sleep(2*time.Second)
	}

}
func producer2 (c chan int, cName int) {
	for {
		rand.Seed(time.Now().UnixNano())
		i := rand.Intn(1000)
		c<-i
		fmt.Printf("生产者 %d put ID: %d\n", cName, i)
		time.Sleep(2*time.Second)
	}

}
func consumer(c chan int) {
	for p:= range c {
		fmt.Printf("消费者 get ID: %d\n", p)
		time.Sleep(5*time.Second)
	}
}

var wg sync.WaitGroup
func main() {
	c := make(chan int)
	//for i:=0;i<3;i++ {
	//	wg.Add(1)
	//	go producer(c, i)
	//	wg.Done()
	//}
	//wg.Wait()
	go producer(c, 1)
	go producer1(c, 2)
	go producer2(c, 3)

	consumer(c)
}

